﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace GKQC.Common
{
    public class DataGridViewSet
    {
        //public readonly static Font DelDataFont = new Font("宋体", 12,FontStyle.Strikeout);
        //public readonly static Font DataFont = new Font("宋体",12, FontStyle.Regular);

        public readonly static Color BadColor = Color.LightPink;

        /// <summary>
        /// 逗号
        /// </summary>
        static readonly char charDot = ',';


        static readonly string BillNo = "BillNo";
        static readonly string ExpireDate = "ExpireDate";
        static readonly string ApproveUser = "ApproveUser";
        static readonly string IsValid = "IsValid";

        public static void BingdingGridData<T>(QcFormBase form, string gridName, List<T> datas)
        {
            if (datas == null) return;
            var c = FormCommon.FindControl(form, gridName);
            if (c != null)
            {
                DataGridView v = c as DataGridView; 
                if (v != null)
                    v.DataSource = new BindingList<T>(datas);
            }
        }

        /// <summary>
        /// 试验到期未检测则试验单号显示红色背景色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void CellFormattingBillNoBackColor(object sender, DataGridViewCellFormattingEventArgs e)
        {
            try
            {
                //不合格值顯示紅色
                DataGridView dataGridView1 = sender as DataGridView;
                if (dataGridView1 == null) return;
                DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                if (cell == null || cell.Value == null) return;
                DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
                if (column == null) return;
                string fieldName = column.DataPropertyName;
                if (string.IsNullOrWhiteSpace(fieldName)) return;

                if (fieldName == BillNo)
                {
                    var bindingItem = dataGridView1.Rows[e.RowIndex].DataBoundItem;
                    PropertyInfo property = bindingItem.GetType().GetProperty(ExpireDate);
                    PropertyInfo propertyApproveUser = bindingItem.GetType().GetProperty(ApproveUser);
                    if (property == null || propertyApproveUser ==null) return;
                    var date = property.GetValue(bindingItem, null);
                    var approveUser = propertyApproveUser.GetValue(bindingItem, null);
                    DateTime dd = DateTime.MinValue;
                    if (DateTime.TryParse(date.ToString(), out dd)
                        && (approveUser == null || string.IsNullOrWhiteSpace(approveUser.ToString()))
                        && dd.Year > 2019 && (dd.Date - DateTime.Now.Date).TotalDays <= 0
                        )
                    {
                        cell.Style.BackColor = BadColor;
                        cell.Style.SelectionForeColor = Color.Black;
                        cell.Style.SelectionBackColor = BadColor;
                    }
                    else
                    {
                        cell.Style.BackColor = dataGridView1.DefaultCellStyle.BackColor;
                        cell.Style.SelectionForeColor = dataGridView1.DefaultCellStyle.SelectionForeColor;
                        cell.Style.SelectionBackColor = dataGridView1.DefaultCellStyle.SelectionBackColor;
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
            }
        }


        
        /// <summary>
        /// 不合格時单号显示红色背景色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void CellFormattingBillNoRedBackColor(object sender, DataGridViewCellFormattingEventArgs e)
        {
            try
            {
                //不合格值顯示紅色
                DataGridView dataGridView1 = sender as DataGridView;
                if (dataGridView1 == null) return;
                DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                if (cell == null || cell.Value == null) return;
                DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
                if (column == null) return;
                string fieldName = column.DataPropertyName;
                if (string.IsNullOrWhiteSpace(fieldName)) return;

                if (fieldName == BillNo)
                {
                    var bindingItem = dataGridView1.Rows[e.RowIndex].DataBoundItem;
                    PropertyInfo property = bindingItem.GetType().GetProperty(IsValid);
                    if (property == null) return;
                    var date = property.GetValue(bindingItem, null);
                    int isvalid = 1;
                    int.TryParse(date.ToString(), out isvalid);
                    if (isvalid ==0)
                    {
                        cell.Style.BackColor = BadColor;
                        cell.Style.SelectionForeColor = Color.Black;
                        cell.Style.SelectionBackColor = BadColor;
                    }
                    else
                    {
                        cell.Style.BackColor = dataGridView1.DefaultCellStyle.BackColor;
                        cell.Style.SelectionForeColor = dataGridView1.DefaultCellStyle.SelectionForeColor;
                        cell.Style.SelectionBackColor = dataGridView1.DefaultCellStyle.SelectionBackColor;
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
            }
        }


        static string isValid = "isValid";
        static string Valid = "Valid";
        static string _2 = "2";
        static string _3 = "3";
        /// <summary>
        /// 不合格显示红色:IQC/IPQC
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void CellFormattingForColorIqc(object sender, DataGridViewCellFormattingEventArgs e)
        {
            try
            {
                DataGridView dataGridView1 = sender as DataGridView;
                if (dataGridView1 == null) return;
                DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                if (cell == null || cell.Value == null) return;
                DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
                if (column == null || !column.Visible) return;
                string fieldName = column.DataPropertyName;
                if (string.IsNullOrWhiteSpace(fieldName)) return;
                var bindingItem = dataGridView1.Rows[e.RowIndex].DataBoundItem;
                if (bindingItem == null) return;
                PropertyInfo property = bindingItem.GetType().GetProperty(fieldName + isValid);
                if (property == null) return;
                var valid = property.GetValue(bindingItem, null);
                bool t = false;
                bool.TryParse(valid.ToString(), out t);
                if (!t)
                {
                    e.CellStyle.ForeColor = Color.Red;
                }
                else
                {
                    e.CellStyle.ForeColor = Color.Black;
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
            }
        }


        /// <summary>
        /// 网格不显示0值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            try
            {
                //不顯示0值
                DataGridView dataGridView1 = sender as DataGridView;
                if (dataGridView1 == null) return;
                DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                if (cell == null || cell.Value == null) return;
                if (e.RowIndex > -1)
                {
                    if ((cell.Value.GetType() == typeof(decimal) 
                        || cell.Value.GetType() == typeof(double) 
                        || cell.Value.GetType() == typeof(int)))
                    {
                        if (Convert.ToDouble(cell.Value) == 0)
                        {
                            e.Value = string.Empty;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
            }
        }

        /// <summary>
        /// 不合格显示红色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void CellFormattingForColor(object sender, DataGridViewCellFormattingEventArgs e)
        {
            try
            {
                //不合格值顯示紅色
                DataGridView dataGridView1 = sender as DataGridView;
                if (dataGridView1 == null) return;
                DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                if (cell == null || cell.Value == null) return;
                DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
                if (column == null) return;
                string fieldName = column.DataPropertyName;
                if (string.IsNullOrWhiteSpace(fieldName)) return;
                var bindingItem = dataGridView1.Rows[e.RowIndex].DataBoundItem;
                PropertyInfo property = bindingItem.GetType().GetProperty(fieldName + isValid);
                if (property == null) return;
                var valid = property.GetValue(bindingItem, null);
                bool t = false;
                bool.TryParse(valid.ToString(), out t);
                if (!t)
                {
                    e.CellStyle.ForeColor = Color.Red;
                }
                else
                {
                    e.CellStyle.ForeColor = Color.Black;
                }

            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
            }
        }

        /// <summary>
        /// 不合格字体加删除线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void CellFormattingStrikeout(object sender, DataGridViewCellFormattingEventArgs e)
        {
            try
            {
                //不合格值顯示紅色
                DataGridView dataGridView1 = sender as DataGridView;
                if (dataGridView1 == null) return;
                DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                if (cell == null || cell.Value == null) return;
                DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
                if (column == null) return;
                string fieldName = column.DataPropertyName;
                if (string.IsNullOrWhiteSpace(fieldName)) return;
                var bindingItem = dataGridView1.Rows[e.RowIndex].DataBoundItem;
                PropertyInfo property = bindingItem.GetType().GetProperty(fieldName + isValid);
                if (property == null) return;
                var valid = property.GetValue(bindingItem, null);
                bool t = false;
                bool.TryParse(valid.ToString(), out t);
                if (!t)
                {
                    Font font = new Font(e.CellStyle.Font.Name, e.CellStyle.Font.Size,FontStyle.Strikeout);
                    e.CellStyle.Font = font;
                }
                else
                {
                    Font font = new Font(e.CellStyle.Font.Name, e.CellStyle.Font.Size);
                    e.CellStyle.Font = font;
                }

            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
            }
        }

        /// <summary>
        /// 画行号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            try
            {
                //行號
                DataGridView grid = sender as DataGridView;
                if (grid == null) return;

                System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(e.RowBounds.Location.X,
            e.RowBounds.Location.Y, grid.RowHeadersWidth - 4, e.RowBounds.Height);

                TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
                    grid.RowHeadersDefaultCellStyle.Font,
                    rectangle,
                    grid.RowHeadersDefaultCellStyle.ForeColor,
                    TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
            }
            catch(Exception ex)
            {
                Logger.LogError(ex);
            }
        }

        /// <summary>
        /// 设置多个列的可见性
        /// </summary>
        /// <param name="dataPropertyNames">检测项目代号类别(逗号分隔)</param>
        /// <param name="isVisible"></param>
        public static void SetColumnsVisible(DataGridView grid, string dataPropertyNames, bool isVisible)
        {
            if (string.IsNullOrWhiteSpace(dataPropertyNames))
                return;

            try
            {
                List<string> names = new List<string>(dataPropertyNames.Split(charDot));
                //List<DataGridViewColumn> delCols = new List<DataGridViewColumn>();
                foreach (DataGridViewColumn c in grid.Columns)
                {
                    if (string.IsNullOrWhiteSpace(c.DataPropertyName))
                        continue;

                    if (names.Contains(c.DataPropertyName))
                    {
                        c.Visible = isVisible;
                        //c.DisplayIndex = names.IndexOf(c.DataPropertyName) + 1;
                    }
                    else
                    {
                        c.Visible = !isVisible;
                        //c.DisplayIndex = grid.Columns.Count -1;
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
            }
        }

        /// <summary>
        /// 设置多个列的可见性
        /// </summary>
        /// <param name="items">检测项目代号</param>
        /// <param name="isVisible"></param>
        public static void SetColumnsVisible(DataGridView grid, List<string> items, bool isVisible)
        {
            if (items == null || items.Count ==0)
                return;

            try
            {
                foreach (DataGridViewColumn c in grid.Columns)
                {
                    if (string.IsNullOrWhiteSpace(c.DataPropertyName))
                    {
                        continue;
                    }
                        

                    if (items.Contains(c.DataPropertyName))
                    {
                        c.Visible = isVisible;
                        //c.DisplayIndex = names.IndexOf(c.DataPropertyName) + 1;
                    }
                    else
                    {
                        c.Visible = !isVisible;
                        //c.DisplayIndex = grid.Columns.Count -1;
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
            }
        }
    }


}
